﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain.Models;
using Domain.DAL;
using Utilities;
using Casanova.Web;

namespace WebApp.ProductTypes
{
    public partial class ProductTypeEdit : System.Web.UI.Page
    {

        #region Private Properties

        public int ProductTypesID
        {
            get
            {
                if (this.ViewState["ID"] != null)
                {
                    return Convert.ToInt32(this.ViewState["ID"]);
                }
                return MyConfig.NullNumber;
            }
            set
            {
                this.ViewState["ID"] = value.ToString();
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                (Master as SiteMaster).SelectedTab = "systemAdministrator";
                if (!IsPostBack)
                {
                    this.pageTitle.Text = Resources.Strings.SystemAdministrator + @"\" + Resources.Strings.ProductTypeManagement;
                    GetParam();
                    LoadFormForEdit();
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ProductTypesID == MyConfig.NullNumber) //Insert
                {
                    if (InsertProductTypes())
                    {
                        //Utility.ShowMessageBox(this.Page, Resources.Strings.SaveSuccess);
                    }
                    else
                    {
                        Utility.ShowMessageBox(this.Page, Resources.Strings.SaveFail);
                        return;
                    }
                }
                else     //Update
                {
                    if (UpdateProductTypes())
                    {
                        //Utility.ShowMessageBox(this.Page, Resources.Strings.UpdateSuccess);
                    }
                    else
                    {
                        Utility.ShowMessageBox(this.Page, Resources.Strings.UpdateFail);
                        return;
                    }
                }
                Response.Redirect(this.ResolveUrl("~/ProductTypes/ProductTypeManagement.aspx"));
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.ResolveUrl("~/ProductTypes/ProductTypeManagement.aspx"));
        }

        #endregion

        #region Private Methods

        private bool CheckValidate()
        {
            if (string.IsNullOrEmpty(txtProductTypeName.Text.Trim()))
            {
                return false;
            }
            return true;
        }

        private bool InsertProductTypes()
        {
            bool result = false;
            try
            {
                if (!CheckValidate())
                {
                    result = false;
                }
                else
                {
                    ProductTypeDAO proDAO = ProductTypeDAO.GetInstance();
                    ProductType p = GetProductTypesInfo();
                    //Insert Employee.
                    int temp = proDAO.Insert(p);
                    if (temp != MyConfig.NullNumber)
                    {
                        this.ProductTypesID = temp;
                        result = true;
                    }
                }
            }
            catch (Exception)
            {
                result = false;
                throw;
            }
            return result;
        }

        private bool UpdateProductTypes()
        {
            bool result = false;
            try
            {
                if (!CheckValidate())
                {
                    result = false;
                }
                ProductTypeDAO pDAO = ProductTypeDAO.GetInstance();
                ProductType p = GetProductTypesInfo();
                p.ID = ProductTypesID;
                //Update Employee.
                result = pDAO.Update(p);
            }
            catch (Exception)
            {
                result = false;
                throw;
            }
            return result;
        }

        private ProductType GetProductTypesInfo()
        {
            ProductType p = new ProductType();
            try
            {
                p.ProductTypeName = txtProductTypeName.Text.Trim();
                p.DisplayColor = cpDisplayColor.Color;
            }
            catch (Exception)
            {
                p = null;
                throw;
            }
            return p;
        }

        private void GetParam()
        {
            this.ProductTypesID = QueryStringUtility.GetInt32("ID", MyConfig.NullNumber);
        }

        private void LoadFormForEdit()
        {
            try
            {
                ProductTypeDAO pDAO = ProductTypeDAO.GetInstance();
                ProductType p = new ProductType();
                p = pDAO.Get(this.ProductTypesID);

                if (p != null)
                {
                    txtProductTypeName.Text = p.ProductTypeName;
                    cpDisplayColor.Color = p.DisplayColor;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

    }
}